package ftp

import (
	"fmt"
	"gin/model"
	"strconv"
	"strings"
	"time"
)

// 登录日志
func (*DataParsing) parsingLoginLog(catData []string, ftpInfoId int64) []model.DataLogin {
	var logDate, LogTime, steamId, gameName, gameId, loginType, loginIp, location string
	var end int
	var dataArr []model.DataLogin
	for _, logTxt := range catData {
		//fmt.Println("原始数据", logTxt)

		parts := strings.Split(logTxt, ":")
		//fmt.Println("分割数据", parts)
		timeParts := strings.Split(parts[0], "-")
		//fmt.Println("分割数据2", timeParts)
		//日期转换  年-月-日
		logDate = strings.Replace(timeParts[0], ".", "-", -1)
		//fmt.Println("转换数据1", catDate)
		//时间转换  时:分:秒
		Time := strings.Replace(timeParts[1], ".", ":", -1)
		//fmt.Println("转换数据2", catTime)
		layout := "2006-01-02 15:04:05"
		// 解析时间字符串
		//fmt.Println("解析时间格式为", catDate+" "+catTime)
		t, err := time.Parse(layout, logDate+" "+Time)
		if err != nil {
			fmt.Println("解析时间格式为", err.Error())
			LogTime = "0"
			//return
		}
		// 转换为 Unix 时间戳（秒）

		LogTime = strconv.FormatInt(t.Unix(), 10)
		logTime, _ := strconv.ParseInt(LogTime, 10, 64)
		//太平洋时间制  + 8 小时
		//logTime = logTime + 3600*8
		t = time.Unix(logTime, 0)
		logDate = t.Format("2006-01-02")

		hour, minute, second := t.Clock()
		Time = fmt.Sprintf("%02d:%02d:%02d", hour, minute, second)

		//
		//Steamed 获取
		// 查找第一个单引号的位置
		/*start := strings.Index(logTxt, ": '")
		start = strings.Index(logTxt[start+4:], " ")
		if start < 0 {
			steamId = ""
		} else {
			// 查找第二个单引号的位置
			end := strings.Index(logTxt[start+1:], ":")
			if end < 0 {
				steamId = ""
			} else {
				// 提取引号内的数据
				steamId = logTxt[start+1 : start+1+end]
			}
		}*/

		start := strings.Index(logTxt, ": '")
		//fmt.Println("位置1", start)
		if start < 0 {
			steamId = ""
		} else {
			starts := strings.Index(logTxt[start+3:], " ")
			//fmt.Println("位置2", starts)
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[starts+3+start+1:], ":")
			if end < 0 {
				steamId = ""
			} else {
				steamId = logTxt[starts+3+start+1 : starts+3+start+1+end]
			}
		}

		//fmt.Println("steamid", steamId)

		//游戏名名称获取
		start = strings.Index(logTxt, ":")
		//fmt.Println("位置1", start)
		if start < 0 {
			gameName = ""
		} else {
			starts := strings.Index(logTxt[start+1:], ":")
			//fmt.Println("位置2", starts)
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[starts+1+start+1:], "(")
			if end < 0 {
				gameName = ""
			} else {
				gameName = logTxt[starts+1+start+1 : starts+1+start+1+end]

				gameName = TruncateStringByCharCount(gameName, 50)

				// 输出字节集（以十六进制表示）
				//fmt.Printf("Byte slice: %x\n", byteSlice)

				// 将字节集转换回字符串
				gameName = filterInvalidChars(gameName)
			}
		}

		//游戏名id获取
		start = strings.Index(logTxt, "(")
		if start < 0 {
			gameId = ""
			//fmt.Println("找不到单引号")
			//return
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+1:], ")")
			if end < 0 {
				gameId = ""
				//fmt.Println("找不到第二个单引号")
				//return
			} else {
				// 提取引号内的数据
				gameId = logTxt[start+1 : start+1+end]
			}
		}

		// 登录类型获取
		start = strings.Index(logTxt, ")' ")
		if start < 0 {
			loginType = "0"
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+3:], ": ")
			if end < 0 {
				loginType = "0"
			} else {
				loginType = logTxt[start+3 : start+3+end]
			}
		}
		//fmt.Println("类型为:", catType)
		// 登录ip获取
		start = strings.Index(logTxt, ": '")
		if start < 0 {
			loginIp = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+3:], " ")
			if end < 0 {
				loginIp = ""
			} else {
				loginIp = logTxt[start+3 : start+3+end]
			}
		}

		start = strings.Index(logTxt, ": '")
		starts := strings.Index(logTxt[start+3:], ": ")
		if start < 0 {
			location = ""
		} else {
			// 查找第二个单引号的位置
			location = logTxt[start+3+starts+2:]
		}

		//fmt.Println("数据为：", ftpInfoId, logDate, Time, LogTime, steamId, gameName, gameId, loginType, loginIp, location)

		gameId, _ := strconv.ParseInt(gameId, 10, 64)
		downloadFile := model.DataLogin{FtpInfoId: ftpInfoId, Date: logDate, Time: Time, LogTime: logTime, SteamId: steamId, GameName: gameName, GameId: gameId, LoginType: loginType, LoginIp: loginIp, Location: location, RegTime: logTime}
		dataArr = append(dataArr, downloadFile)
	}
	return dataArr
}
